package com.gress.shop.controller.member;

import com.gress.shop.bo.Member;
import com.gress.shop.constants.PopConstants;
import com.gress.shop.service.IMemberService;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api("忘记密码")
@Controller
@RequestMapping("member")
public class ForgetController {
    @Resource
    private IMemberService memberService;

    @RequestMapping("forget_psd")
    public String forgetPsd() {
        return "member/forget_psd";
    }


    @RequestMapping("send_code_forget_psd")
    public String sendCodeForgetPsd(Model model,
                                    @ModelAttribute("email") String email,
                                    HttpServletRequest request) {
        HttpSession session = request.getSession();
        SwalReturn swalReturn = memberService.sendForgetEmail(email, session);
        if (swalReturn.isSuccess()) {
            return "member/forget_psd2";
        }else {
            model.addAttribute(swalReturn);
            return "member/forget_psd";
        }
    }

    @RequestMapping("update_psd")
    public String updatePsd(Model model,
                            Member member,
                            @ModelAttribute("checkPsd") String checkPsd,
                            String code,
                            HttpSession session) {
        if (StringUtils.isEmpty(checkPsd) || !checkPsd.equals(member.getPassword())) {
            //密码校验失败
            model.addAttribute("email", member.getEmail());
            model.addAttribute(PopConstants.swalCode, SwalFactory.getWarnSwal("两次输入的密码错误"));
            return "member/forget_psd2";
        }
        SwalReturn swalReturn = memberService.updateMemberPsd(member, code, session);
        if (swalReturn.isSuccess()) {
            return "member/forget_psd3";
        }else {
            model.addAttribute("email", member.getEmail());
            model.addAttribute(swalReturn);
            return "member/forget_psd2";
        }
    }




}
